# -*- coding:utf-8 -*-
import sys

reload(sys)

sys.setdefaultencoding('utf-8')
from django.shortcuts import render
from libs.base.baseview import BaseView
from models import Article,CommentFrist,CommentSecondary,Plate,Label
from serializers import ArticleCreateSerializers, ArticleSerializers,CommentFristSerializers,\
    CommentSecondarySerializers,PlateArticleSerializers,PlateSerializers,LabelArticleSerializers,LabelSerializers
from django.db.models import Q
from rest_framework import status
from rest_framework.parsers import JSONParser,MultiPartParser, FormParser,FileUploadParser
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication
from rest_framework import permissions
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from permissions import IsOwnerOrReadOnly


class ArticleViewset(BaseView):
    '''
    文章
    '''
    queryset = Article.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permissions = [permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly]
    # parser_classes = (FormParser,FileUploadParser)
    def get_serializer_class(self):
        if self.action == "retrieve":
            return ArticleSerializers
        elif self.action == "create":
            return ArticleCreateSerializers

        return ArticleSerializers


    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.visit_number += 1
        instance.save()
        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def update(self, request, *args, **kwargs):
        if request.data['thumb']:
            instance = self.get_object()
            if request.data['thumb'] == 'add':
                instance.thumb_number += 1
            else:
                instance.thumb_number -= 1
            instance.save()
            serializer = self.get_serializer(instance)
            return Response(serializer.data)
        else:
            super(ArticleViewset,self).update(request, *args, **kwargs)


class CommentFristViewset(BaseView):
    '''
    一级评论
    '''
    queryset = CommentFrist.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permissions = permissions.IsAuthenticated()
    CommentFristSerializers = CommentFristSerializers
    def update(self, request, *args, **kwargs):
        if request.data['thumb']:
            instance = self.get_object()
            instance.thumb_number += 1
            instance.save()
            serializer = self.get_serializer(instance)
            return Response(serializer.data)
        else:
            super(CommentFristViewset,self).update(request, *args, **kwargs)


class CommentSecondaryViewset(BaseView):
    '''
    二级评论
    '''
    queryset = CommentSecondary.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    CommentFristSerializers = CommentSecondarySerializers
    permissions = permissions.IsAuthenticated()
    def update(self, request, *args, **kwargs):
        if request.data['thumb']:
            instance = self.get_object()
            instance.thumb_number += 1
            instance.save()
            serializer = self.get_serializer(instance)
            return Response(serializer.data)
        else:
            super(CommentSecondaryViewset,self).update(request, *args, **kwargs)



class PlateViewset(BaseView):
    '''
    板块
    '''
    queryset = Plate.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permissions = permissions.IsAuthenticatedOrReadOnly()
    def get_serializer_class(self):
        if self.action == "create":
            return PlateSerializers
        return PlateArticleSerializers



class LabelViewset(BaseView):
    '''
    标签
    '''
    queryset = Label.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permissions = permissions.IsAuthenticated()
    def get_serializer_class(self):
        if self.action == "create":
            return LabelSerializers
        return LabelArticleSerializers




